package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.UserArchive;
import com.biovr.backend.repository.UserArchiveRepository;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "/archives",description = "游戏存档API", consumes="application/json")
@RestController
@RequestMapping("/archives")
public class UserArchiveRestController {
    @Autowired
    UserArchiveRepository userArchiveRepository;
    @Autowired
    DateTimeUtils dateTimeUtils;
    @GetMapping
    public RestResponse<UserArchive> getByUserAndExpm(@RequestParam String userId,@RequestParam String expmId){
        return ResponseFactory.createSuccessResult(userArchiveRepository.findByExpmIdAndUserId(expmId,userId));
    }
    @PostMapping()
    public RestResponse saveUserArchive(@RequestBody UserArchive userArchive){
        UserArchive tarResult = userArchiveRepository.findByExpmIdAndUserId(userArchive.getExpmId(),userArchive.getUserId());
        //累加提交
        if(tarResult!=null){
            userArchive.setId(tarResult.getId());
        }
        userArchive.setCreateTime( dateTimeUtils.getCurrDateTimeString());
        return ResponseFactory.createSuccessResult(userArchiveRepository.save(userArchive));
    }
}
